package top.ikunikun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.ikunikun.common.Result;
import top.ikunikun.domain.Category;
import top.ikunikun.domain.Dish;
import top.ikunikun.domain.Setmeal;
import top.ikunikun.dto.SetmealDto;
import top.ikunikun.service.CategoryService;
import top.ikunikun.service.SetmealService;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 */
@Slf4j
@ResponseBody
@RestController
@RequestMapping(("/setmeal"))
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 查询套餐类别中的套餐
     * @param dish
     * @return
     */
    @GetMapping("/list")
        public Result<List<Setmeal>> get(Dish dish){
        log.info(dish.toString());
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Setmeal::getCategoryId,dish.getCategoryId());
        List<Setmeal> list = setmealService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public Result<String> save(@RequestBody SetmealDto setmealDto){
        log.info("/setmeal: {}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return Result.success("新增套餐成功");
    }

    /**
     * 分页查询套餐列表
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Result<Page<SetmealDto>> page(int page,int pageSize,String name){
        Page<Setmeal> setmealPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null,Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(setmealPage,queryWrapper);
        Page<SetmealDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(setmealPage,dtoPage,"records");
        List<Setmeal> records = setmealPage.getRecords();
        List<SetmealDto> setmealDtos = records.stream().map(item -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            // 分类id
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null){
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());
        dtoPage.setRecords(setmealDtos);
        return Result.success(dtoPage);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public Result<String> delete(@RequestParam List<Long> ids){
        log.info(ids.toString());
        setmealService.removeWithDish(ids);
        return Result.success("套餐删除成功");
    }

    /**
     * 套餐启售状态更改
     * @param num
     * @param ids
     * @return
     */
    @PostMapping("/status/{num}")
    public Result<String> change(@PathVariable int num,@RequestParam List<Long> ids){
        switch (num){
            case 0:
                ids.forEach(id -> {
                    Setmeal setmeal = setmealService.getById(id);
                    setmeal.setStatus(0);
                    Long categoryId = setmeal.getCategoryId();
                    setmealService.updateById(setmeal);
                });
                break;
            case 1:
                ids.forEach(id -> {
                    Setmeal setmeal = setmealService.getById(id);
                    setmeal.setStatus(1);
                    setmealService.updateById(setmeal);
                });
                break;
        }
        return Result.success("停售成功");
    }

}

